package cn.edu.fudan.direct;

import cn.edu.fudan.classifier.Classifiers;
import cn.edu.fudan.type.BeginEndTime;
import cn.edu.fudan.type.DataItem;
import cn.edu.fudan.type.Params;
import org.apache.log4j.Logger;

import java.util.List;

public class ErrorFunction implements AbstractErrorFunction {

	private static Logger logger = Logger.getLogger(ErrorFunction.class);
	
	// the normalization threshold
	private double nThreshold;

	// the data
	private List<DataItem> tsData;
	private BeginEndTime bet;
	
	private Classifiers classifiers;

	public ErrorFunction(List<DataItem> data, double nThreshold, BeginEndTime bet) {

		this.tsData = data;
		this.nThreshold = nThreshold;
		this.bet = bet;
		this.classifiers = new Classifiers(data, bet);
	}

	@Override
	public double valueAt(Point point) {
		// TODO Auto-generated method stub

		double[] coords = point.toArray();
		int windowSize = Long.valueOf(Math.round(coords[0])).intValue();
		int paaSize = Long.valueOf(Math.round(coords[1])).intValue();

		if (paaSize > windowSize) {
			return 1.0d;
		}

		Params params = new Params(windowSize, paaSize, this.nThreshold);
		logger.debug(params.toString());
		
		// validation phase
		// Classifying...
        // is this sample correctly classified?
		double accuracy =  classifiers.PAMAPClassifier(params);	
		double error = 1 - accuracy;
		return error;
	}

}
